package com.hlx.controller.user;

import com.hlx.entity.admin.TrainNumberSeat;
import com.hlx.entity.user.Ticket;
import com.hlx.service.admin.TrainNumberSeatService;
import com.hlx.service.user.OrderFormService;
import com.hlx.service.user.TicketService;
import com.hlx.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ticket")
public class TicketController {
    @Autowired
    private TicketService ticketService;
    @Autowired
    private OrderFormService orderService;
    @Autowired
    private TrainNumberSeatService trainNumberSeatService;


    @PostMapping("/addTicket")
    @Transactional
    public Result<String> addTicket(@RequestBody List<Ticket> tickets){
        log.info("{}",tickets);
        Iterator it = tickets.iterator();
        boolean res = true;
        while (it.hasNext()){
            Ticket ticket = (Ticket) it.next();
            ticket.setDate(ticket.getDate().plusDays(1));
            res = ticketService.save(ticket);
            log.info("{}",tickets);
            if(!res || ticketService.getIdByTicketName(ticket.getTrainId(),ticket.getPersonId(), ticket.getDate()) == 0){
                return Result.error("购买失败");
            }
            res = orderService.addOrder(ticketService.getIdByTicketName(ticket.getTrainId(),ticket.getPersonId(), ticket.getDate()));
            if(!res){
                return Result.error("购买失败");
            }
        }
        log.info("{}",tickets);
        TrainNumberSeat trainNumberSeat = trainNumberSeatService.gerTrainNumberSeatByDateAndTrainNumberId(tickets.get(0).getDate(),tickets.get(0).getTrainId());
        if(tickets.get(0).getSeatType() == 0){
            trainNumberSeat.setBusiness(trainNumberSeat.getBusiness() - tickets.size());
        }else if(tickets.get(1).getSeatType() == 1){
            trainNumberSeat.setFirst(trainNumberSeat.getFirst() - tickets.size());
        }else {
            trainNumberSeat.setSecond(trainNumberSeat.getSecond() - tickets.size());
        }
        trainNumberSeatService.updateById(trainNumberSeat);
        return Result.success("购买成功！");
    }

    @Transactional
    @PostMapping("/delteTicket/{ticketId}")
    public Result<String> deleteTicket(@PathVariable Integer ticketId){
        Ticket ticket = ticketService.getById(ticketId);
        orderService.deleteOrder(ticketId);
        ticketService.removeById(ticketId);
        trainNumberSeatService.UpdateTrainNumberSeat(ticket.getTrainId(),ticket.getDate(),ticket.getSeatType());
        return Result.success("退票成功");
    }

    @PutMapping("/updateTicket")
    public Result<String> updateTicket(@RequestBody Ticket ticket){
        log.info("{}",ticket);
        if(!ticketService.updateById(ticket)){
            return Result.error("改签失败");
        }
        return Result.success("改签成功");
    }


}
